package sw6.launcher;

import sw6.admin.R;
import sw6.admin.internal.Definitions;
import sw6.girafAppKit.GirafActivity;
import sw6.lib.Settings;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

/**
 * Main Activity for the launcher. Contains block of the Back key, and interface creation functionality.
 * 
 * @author SW6C
 */
public class launcher extends GirafActivity {
	protected final static String tag = "GIRAF"; 

	/** Code for black background color in the launcher */
	private final int Black = 1;
	
	/** Code for white background color in the launcher */
	private final int White = 2;
	
	/** Code for red background color in the launcher */
	private final int Red = 3;
	
	/** Code for green background color in the launcher */
	private final int Green = 4;
	
	/** Code for blue background color in the launcher */
	private final int Blue = 5;
	
	/** Code for yellow background color in the launcher */
	private final int Yellow = 6;

	/** 
	 * Called when the activity is first created. 
	 *  Populates the GridView of applications and sets the onClickListener
	 *  used for opening the applications.
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.launcher);

		// SW6b code start
		if (isFirstRun()) {
			startFirstRunGui();
		}
		// SW6b code end

		GridView gridview = (GridView) findViewById(R.id.gridView);
		gridview.setAdapter(IconAdapter.getInstance(this));

		gridview.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View v, int position, long id) {

				String mInfo = (String)parent.getItemAtPosition(position);

				try {
					Intent intent = getPackageManager().getLaunchIntentForPackage(mInfo);
					intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
					startActivity(intent);
				} catch (Exception e) {
					Toast.makeText(launcher.this, "There was an error: " + e.getMessage(), Toast.LENGTH_LONG).show();
				}
			}
		});
	}

	@Override
	protected void onResume() {
	    super.onResume();
	    
		switch(Settings.getEnum(this, "bgcolor")) {
		case Black:
			findViewById(R.id.MainLayout).setBackgroundColor(Color.BLACK);
			break;
		case White:
			findViewById(R.id.MainLayout).setBackgroundColor(Color.WHITE);
			break;
		case Red:
			findViewById(R.id.MainLayout).setBackgroundColor(Color.RED);
			break;
		case Green:
			findViewById(R.id.MainLayout).setBackgroundColor(Color.GREEN);
			break;
		case Blue:
			findViewById(R.id.MainLayout).setBackgroundColor(Color.BLUE);
			break;
		case Yellow:
			findViewById(R.id.MainLayout).setBackgroundColor(Color.YELLOW);
			break;

		}

		((IconAdapter)((GridView) findViewById(R.id.gridView)).getAdapter()).updateApps();
	}

	// SW6b code start
	private boolean isFirstRun() {
		boolean isFirstRun = Settings.getBoolean(launcher.this, "isFirstRun");
		return isFirstRun;
	}

	private void startFirstRunGui() {

		Intent intent = new Intent();
		intent.setClassName(Definitions.SW6_ADMIN_PACKAGE_NAME, "sw6.admin.gui.FirstRunActivity");
		startActivity(intent);
	}
	// SW6b code end

	/**
	 * This comes from the home example, and disables the backkey for the launcher.
	 * @param keyCode the code of the key pressed.
	 * @param event The KeyEvent object that defines the button action.
	 * @return true if handled, else false.
	 */
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		switch (event.getKeyCode()) {
		case KeyEvent.KEYCODE_BACK:
			return true;
		case KeyEvent.KEYCODE_HOME:
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
	
   /**
     * This comes from the home example, and disables the backkey for the launcher.
     * @param keyCode the code of the key pressed.
     * @param event The KeyEvent object that defines the button action.
     * @return true if handled, else false.
     */
	@Override
	public boolean onKeyUp(int keyCode, KeyEvent event) {
		switch (event.getKeyCode()) {
		case KeyEvent.KEYCODE_BACK:
			return true;
		case KeyEvent.KEYCODE_HOME:
			return true;
		}
		return super.onKeyUp(keyCode, event);
	}
}